package com.eiokey.baselibrary.base.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.eiokey.baselibrary.annotation.AnnotationHandler;
import com.eiokey.baselibrary.annotation.BindingVariable;
import com.eiokey.baselibrary.annotation.GetLayoutId;
import com.eiokey.baselibrary.utils.BaseUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;


/**
 * description  Activity基类
 * use
 * param
 * return
 * @author JetQiao
 * @date 2021/7/21  5:44 下午
 */
public class BaseActivity<VM extends BaseViewModel, V extends ViewDataBinding> extends AppCompatActivity {
    protected List<Disposable> disposables = new ArrayList<>();
    protected VM mViewModel;
    protected V mDataBinding;
    protected View mRootView;
    protected Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        beforeBindData(savedInstanceState);
        bindViewModel();
        initView(savedInstanceState);
    }


    /**
     * description  设置layoutId,如果使用注解则可以不用重写该方法
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/21  6:45 下午
     */
    protected int setLayoutId() {
        return 0;
    }


    /**
     * description  取layoutId
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/21  6:45 下午
     */
    private int getLayoutId() {
        // 避免反射
        GetLayoutId layoutId = getClass().getAnnotation(GetLayoutId.class);
        if (layoutId != null) {
            return layoutId.value();
        } else {
            return setLayoutId();
        }
    }

    /**
     * description      获取BR值
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/26  16:31
     */
    private int getBindingVariableId(Object object) {
        if ((object instanceof Activity || object instanceof Fragment || object instanceof Dialog)) {
            Class<?> objClass = object.getClass();
            BindingVariable contentView = objClass.getAnnotation(BindingVariable.class);
            if (contentView != null) {
                return contentView.value();
            }
        }
        return -1;
    }

    /**
     * description  在绑定数据之前，setContentView之前
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/21  6:51 下午
     */
    protected void beforeBindData(Bundle bundle) {
        //去掉标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉信息栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }


    /**
     * description  在绑定数据之后，setContentView之后,重写后用作初始化数据
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/21  6:51 下午
     */
    protected void initView(Bundle bundle) {
    }


    /**
     * description  初始化页面和绑定viewModel
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/21  6:52 下午
     */
    protected void bindViewModel() {
        int layoutId = getLayoutId();
        if (layoutId == 0) {
            throw new RuntimeException("页面不能为空!!");
        }
        mDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
        mRootView = mDataBinding.getRoot();
        Class<VM> viewModelClass = AnnotationHandler.getViewModelClass(this);
        if (viewModelClass != null) {
            mViewModel = new ViewModelProvider(getViewModelStore(), ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(viewModelClass);
            mDataBinding.setVariable(getBindingVariableId(this), mViewModel);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        //        this.wakeLock.acquire();
        if (Build.VERSION.SDK_INT < 19) {
            Intent intent = new Intent();
            intent.setAction("com.android.systembar.hide");
            sendBroadcast(intent);
        }

        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT < 19) {
            // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else {
            Window window = getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
            window.setAttributes(params);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //        this.wakeLock.release();
    }

    @Override
    protected void onDestroy() {
        // 取消订阅
        for (Disposable disposable : disposables) {
            BaseUtils.dispose(disposable);
        }
        super.onDestroy();

        if (Build.VERSION.SDK_INT < 19) {
            Intent intent = new Intent();
            intent.setAction("com.android.systembar.show");
            sendBroadcast(intent);
        }
    }
}
